function Game(){
	this.players = new Array();
	this.total_player = 0;
	this.myid = 0;
	this.uid = 0;
	
	this.phase = 0;
	this.state = 0;
	this.round = 0;	//refer to wiki STATE TURN
	this.turn = 0;  //refer to wiki STATE player
	
	this.current_message_info;
	
	this.card = new Array();

	var _myself = this;
	
	this.init = function(){
		if (this.card.length == 0) {
			this.card[STATE_MESSAGE] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14];
			this.card[STATE_FUNCTION] = [6, 8, 9, 10, 11, 12, 13, 14];
			this.card[STATE_TRANSMIT_M] = [1, 7];
			this.card[STATE_TRANSMIT_O] = [1, 2];
			this.card[STATE_ARRIVED] = [3, 5];
			this.card[STATE_BLOCK]   = [4];
			this.card[STATE_WAITING] = [];
		}		
	}

	this.SELECTED_CARD			= -1;
	this.SELECTED_TARGET		= -1;
	
	this.getPlayer = function(pid){
		if(!pid) pid = _myself.uid;
		var player = players[pid - 1 ];
		return player; 
	}
	
	this.addPlayer = function(player){
		_myself.players.push(player);
		_myself.total_player = _myself.players.length;
	};
	
	this.getPlayerIds = function(){
		var ret =new Array();
		for(var i = 0; i< _myself.total_player; i++ ){
			if(i !=_myself.myid)
				ret.push(i );
		}
		return ret;
	};
	
	this.getPlayerIdsWithoutLock = function(){
		var ret =new Array();
		for(var i = 0; i< _myself.total_player; i++ ){
			if(i !=_myself.myid)//TODO NOT LOCK
				ret.push(i);
		}
		return ret;
	};
}

function Player(_id,_role,_name,_image){
	this.id = _id;
	this.role = _role;
	this.name   = _name;
	this.image  = _image;
	this.status;

	this.messageCards = new Array();		//Array of Object Card
	this.functionCards = new Array();		//Array of Object Card
	var myself = this;
	
	/* New message card received */
	this.addMessageCard = function(card){
		myself.messageCards.push(card);
	}
	
	this.addFunctionCard = function(card){
		myself.functionCards.push(card);
	}
	
	this.clearMessageCard = function(){
		while(myself.messageCards.length >0){
			myself.messageCards.pop();
		}
	}
	
	this.clearFunctionCard = function(){
		while(myself.functionCards.length >0){
			myself.functionCards.pop();
		}		
	}
	
	this.getCardWithFunction = function(functions){
		var ret = new Array();
		for(var i=0;i<myself.functionCards.length;i++){
			var card = myself.functionCards[i];
			if(array_exist(functions,card.fid)){
				ret.push(card.id);
			}
		}
		return ret;
	}
	
	var getCardsByColor = function(color){
		var cards = new Array();
		for(var i=0;i<messageCards.length;i++){
			if(messageCards[i].color == color)
				cards.push(messageCards[i]);
		}
		return cards;
	}
	
	this.getRedCardNo = function (){
		return getCardsByColor(RED_CARD).length;
	}
	
	this.getBlueCardNo = function (){
		return getCardsByColor(RED_BLUE).length;
	}
	
	this.getBlackCardNo = function (){
		return getCardsByColor(RED_BLACK).length;
	}
	
	this.getFunctionCardNo = function (){
		return functionCards.length;
	}
}

var _cardsDB = new Array();
function getCard(_id){
	if(!_cardsDB[_id]){
		_cardsDB[_id] = new Card(_id);
	}
	return _cardsDB[_id];
}

function Card(_id){
	var myself = this;

	myself.id = _id;
	
	var KEY_ID = 0;
	var KEY_NAME = 1;
	var KEY_FUNCTION = 2;
	var KEY_TYPE = 3;
	var KEY_TID = 4;
	var KEY_DESC = 5;
	var KEY_COLOR = 6;
	var KEY_LOCATION = 7;
	
	myself.card = CARD_DATABASE.cards[_id -1];
	//myself.id   = myself.card[KEY_ID];
	myself.name = myself.card[KEY_NAME];
	myself.fid 	= myself.card[KEY_FUNCTION];
	myself.type = myself.card[KEY_TYPE];
	myself.tid  = myself.card[KEY_TID];
	myself.desc = myself.card[KEY_DESC];
	myself.color = myself.card[KEY_COLOR];
	myself.img_location = myself.card[KEY_LOCATION];
}